﻿'use strict';
app
    // Login Box controller 
    .controller('LogoutController', [
        '$rootScope', '$scope', '$state', '$localStorage', '$timeout', '$http', '$modal', 'commonFactory',
        function ($rootScope, $scope, $state, $localStorage, $timeout, $http, $modal, commonFactory) {
            //clear session                        
            if ($localStorage.user.UserId <= 0) {
                //$state.go('login');
                $scope.UserName = $localStorage.UserName;
            } else if ($localStorage.user.UserId > 0) {
                $scope.UserName = $localStorage.user.UserName;
                ClearSession();
            } else {
                $scope.UserName = $localStorage.UserName;
            }
            $scope.title = "Login again page",
            $scope.messeger = "",           
            $scope.popupLogin = function (windowClass) {
                $modal.open({
                    windowClass: windowClass,
                    templateUrl: "WarningModal",
                    controller: "LoginModalInstanceCtrl",
                    size: "sm",
                    resolve: {
                        messeger: function () {
                            return $scope.messeger;
                        },
                        title: function () {
                            return $scope.title;
                        }
                    }
                })
            }
            function ClearSession() {
                commonFactory.ClearSession($localStorage.user.UserId).then(function (resp) {
                    if (resp.data.Status) {                        
                        $localStorage.user.UserId = 0;
                        $localStorage.UserName = $localStorage.user.UserName;
                        //$timeout(function () {
                        //    $state.go('login');
                        //}, 100);
                    }
                }, function (error) {
                    $scope.status = 'Unable to check session data: ' + error.message;
                });
            }
            $scope.doLogin = function (password) {                
                if (password == '' || password==null) {
                    $scope.title = 'Login';
                    $scope.messeger = 'Please enter Password';                    
                    $scope.popupLogin('modal-message modal-warning');
                } else {
                    $http.post($rootScope.APIAccount, { username: $scope.UserName, password: password }).then(
                        function successCallback(resp) {
                            if (resp.data.UserName != null) {
                                GetUser(resp.data.UserId);
                            } else {
                                $scope.messeger = 'Login was unsuccessful. Please correct the errors and try again',
                                $scope.title = 'Warning',
                                $scope.popupLogin('modal-message modal-warning');
                            }
                        }, function errorCallback(resp) {
                            //err.status will contain the status code                      
                            $scope.messeger = 'Login was unsuccessful. You are not authorized. Please speak to your administrator';
                            $scope.title = 'Warning';
                            $scope.popupLogin('modal-message modal-warning');
                        }
                  );
                }
            };
            function GetUser(UserId) {
                commonFactory.getUserById(UserId).then(function (response) {
                    $localStorage.user = response.data;
                    $rootScope.token = $localStorage.user.SessionId;
                    console.log($rootScope.token)
                    $state.go('app.dashboard');
                }, function (error) {
                    $scope.status = 'Unable to load profile data: ' + error.message;
                });
            }
        }
    ])

app.controller('LoginModalInstanceCtrl', function ($scope, $modalInstance, messeger, title) {
    $scope.messeger = messeger;
    $scope.title = title;
    $scope.ok = function () {
        $modalInstance.close();
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});


